﻿using UnityEngine;
using System.Collections;

public class MultiplySpriteColorChildren : MonoBehaviour {

	public Color    []                MulStartColor;
	public Color                    MulEndColor = Color.gray;
	public float                    Speed = 1.0f;
	
	UISprite []                     m_nguiSprite;
	float                           m_rAccuTime = 0.0f;
	Color    []                       m_colCur;



	public void SetToGray()
	{
		this.m_rAccuTime = 0.0f;
		m_nguiSprite = transform.GetComponentsInChildren<UISprite>();
		MulStartColor = new Color[m_nguiSprite.Length];
		m_colCur = new Color[m_nguiSprite.Length];
		for(int i=0; i< m_nguiSprite.Length; i++) {
			MulStartColor[i] = m_nguiSprite[i].color;
			m_colCur[i] = m_nguiSprite[i].color;
		}
		StartCoroutine("CoUpdateColor",false);
	}

	public void BackToOriginal()
	{
		this.m_rAccuTime = 0.0f;
		StartCoroutine("CoUpdateColor",true);
	}

	IEnumerator CoUpdateColor(bool isReverse)
	{

		while(this.m_rAccuTime<=1.0f)
		{
			this.m_rAccuTime += Time.deltaTime * this.Speed;
			for(int i=0; i< m_nguiSprite.Length; i++) {
				if(!isReverse)
					this.m_colCur[i] = Color.Lerp( this.MulStartColor[i], this.MulEndColor, this.m_rAccuTime );
				else
					this.m_colCur[i] = Color.Lerp( this.MulEndColor, this.MulStartColor[i], this.m_rAccuTime );
				m_nguiSprite[i].color = m_colCur[i];
			}
			yield return null;
		}

		if(isReverse)
		{
			for(int i=0; i< m_nguiSprite.Length; i++) {
				m_nguiSprite[i].color = this.MulStartColor[i];
			}
		}
	}
}
